/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.dao;

import static cn.org.rapid_framework.util.ObjectUtils.isNotEmpty;

import java.util.List;
import java.util.Map;

import javacommon.base.BaseHibernateDao;

import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.tech15.pub.model.PubData;
import com.tech15.pub.vo.query.PubDataQuery;

@Repository
public class PubDataDao extends BaseHibernateDao<PubData,Long>{

	public Class getEntityClass() {
		return PubData.class;
	}
	
	public Page findByPageRequest(PageRequest<Map> pageRequest) {
		//XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
		// [column]为字符串拼接, {column}为使用占位符. 以下为图方便采用sql拼接,适用性能要求不高的应用,使用占位符方式可以优化性能. 
		// [column] 为PageRequest.getFilters()中的key
		String sql = "select t from PubData t where 1=1 "
				+ "/~ and t.name = '[name]' ~/"
				+ "/~ and t.pid = '[pid]' ~/"
				+ "/~ order by [sortColumns] ~/";
		return pageQuery(sql,pageRequest);
	}
	
	public Page findPage(PubDataQuery query) {
		/*String sql = "select t from PubData t where 1=1 "
			  	+ "/~ and t.name = {name} ~/"
			  	+ "/~ and t.pid = {pid} ~/"
				+ "/~ order by [sortColumns] ~/";*/
				
        //生成sql2的原因是为了不喜欢使用xsqlbuilder的同学，请修改生成器模板，删除本段的生成
        StringBuilder sql2 = new StringBuilder("select t from PubData t where 1=1 ");
        if(isNotEmpty(query.getId())) {
            sql2.append(" and  t.id = :id ");
        }
        if(isNotEmpty(query.getName())) {
            sql2.append(" and  t.name = :name ");
        }
        if(isNotEmpty(query.getPid())) {
            sql2.append(" and  t.pid = :pid ");
        }else{
        	sql2.append(" and  t.pid is null ");
        }
        if(isNotEmpty(query.getSortColumns())) {
            sql2.append(" order by :sortColumns ");
        }
        
        
		return pageQuery(sql2.toString(),query);
	}

	public List<PubData> searchByPid(Long pid) {
		String hql = "from PubData t where t.pid=? order by t.sortno asc";
		return getHibernateTemplate().find(hql,pid);
	}

	public List<PubData> findAllParent() {
		String hql = "from PubData t where t.pid is null order by t.sortno asc";
		return getHibernateTemplate().find(hql);
	}

	public List<PubData> searchByPids(Long pid) {
		String hql = "from PubData t where t.pid in (select a.id from PubData a where a.pid="+pid+") order by t.sortno asc";
		return getHibernateTemplate().find(hql);
	}
	

}
